***********************************************
* The Limits and Consequences of Population Policy: 
* Evidence from China's Wan Xi Shao Campaign
* - Kimberly Singer Babiarz 
* - Paul Ma
* - Grant Miller
* - Shige Song
* 
* Replication Code
***********************************************

***********************************************
* Contents
***********************************************

	* (0.0) General setup
		* (0.1) File setup		
		* (0.2) Graphics 
		* (0.3) Begin log
		* (0.4) SSC archive packages 
		* (0.5) Cleaning 
		
	* (1.0)  Unadjusted TFR and SRB change
	* (2.0) Parity Progression
	* (3.0) TFR decline and regression adjusted life tables
	* (4.0) Age at Marriage and Birth Timing 
	* (5.0) Sex Selective Fertility Strategies
	* (6.0) Underreporting

* Note each section (1.0)-(6.0) may be run in parallel.  
	
***********************************************
* (0.0) General setup
***********************************************

	***********************************************
	* (0.1) File  setup
	***********************************************

 	global d_root		`"USER SPECIFY ROOT"'

	global d_do			`"$d_root"'			/* User specify subdirectory for do files if desired 		*/
	global d_data		`"$d_root"'			/* User specify subdirectory for data files if desired 		*/
	global d_out		`"$d_root\Output"'	/* Output saved here		*/
	global d_log		`"$d_root\Log"'		/* Log files saved here		*/
	global d_tab		`"$d_out\Tables"'	/* Tables saved here		*/
	global d_fig		`"$d_out\Figures"'	/* Figures saved here		*/

	cap mkdir `"$d_log"'
	cap mkdir `"$d_out"'
	cap mkdir `"$d_tab"' 
	cap mkdir `"$d_fig"' 
		
	/* Original survey data 										*/
	global f_2PK		`"$d_data\1988_Survey.dta"'							

	/* Analysis datasets: birth-year level and mother-year level panels	*/
	global f_2PKbirthpanel	`"$d_data\2pk_birthpanel.dta"'						
	global f_2PKmotherpanel	`"$d_data\2pk_motherpanel.dta"'						

	***********************************************
	* (0.2) Graphics options 
	***********************************************

	set scheme lean1, permanently		
	graph set window fontface "Times New Roman" 	
	
	***********************************************
	* (0.3) Begin log
	***********************************************
	clear 
	clear matrix
	set mem 1000m
	set more off

    capture log close
	log using `"$d_log\LLF_replication_`c(current_date)'.txt"', text replace
		
	***********************************************
	* (0.4) SSC archive packages 
	***********************************************
	
	cap ssc install outreg2
	cap ssc install boottest
	cap ssc install estout
	cap ssc install unique 

	***********************************************
	* (0.5) Prep analysis datasets 
	***********************************************

	* Don't need run this - analysis datasets provided
	* do `"$d_do\LLF_Cleaning.do"'
	
***********************************************
* (1.0) Unadjusted TFR and SRB change
* 		Figure 1a-b: 	Unadjusted TFR/SRB change
*		Figure A1: Gapminder
* 		Figure A2: Implmentation map		
***********************************************

	import delimited "$d_data\UNPD_WFD_2017_TFR.csv", clear 
		drop if year < 1950
		
		twoway (line tfr year if year <1986,  lcolor(black)) ///
		(pcarrowi 7.3 1970 7.3 1979 , barbsize(1)) (pcarrowi 7.3 1979 7.3 1970, barbsize(1) ) ///
		, legend(off) title(" ") xtitle("Year") ytitle("Total Fertility Rate") ///
		xlab(1950(2)1982, angle(forty_five)) xscale(range(1948 1985)) ///
		text(7.3 1974.5 "LLF Policy" "Implementation" "Range", size(small) box bcolor(white) fcolor(white)) ///
		text(5.5 1982.25 "One Child" "Policy" "(Starting 1980)", size(small) box bcolor(white) fcolor(white)) ///
		text(2.75 1961 "Great Leap" "Famine", size(small)) xline(1970, lpattern(dash)) xline(1979, lpattern(dash))
	
	graph export "$d_fig\Fig_1a_TFR.jpg", replace

	do `"$d_do\LLF_SRB_unadjusted.do"'
	do `"$d_do\LLF_Gapminder.do"'
	do `"$d_do\LLF_Map.do"'

	
*******************************************************
* (2.0) Parity Progression
*		Figures 2a-2d: 	Parity progression risk
* 		Figures A7a-d: 	Secular trends in event time
* 		Figures A9a-d: 	PPR Robustness tests
* 		Figures A10a-d: PPR Robustness tests
* 		Figures A11a-d: PPR Robustness tests
*		Figures A12a-f: PPR Placebo test
*******************************************************

	do `"$d_do\LLF_PPR.do"'
	do `"$d_do\LLF_PPR_Permutation.do"'
	
	do `"$d_do\LLF_PPR_Robustness1.do"'
	do `"$d_do\LLF_PPR_Robustness2.do"'
	do `"$d_do\LLF_Pretrends.do"'

*******************************************************
* (3.0) TFR decline and regression adjusted life tables
* 		Figure 3a-b: 	TFR change
* 		Figure A8: 		First parity ASFR 
* 		Figure A16: 	ASFR heatmap
* 		Table A6: 		Regression adjusted life tables in event time
* 		Table A7: 		Parity specific fertility change in event time
* 		Table A8:		ASFR change in event time
*******************************************************
	
	do `"$d_do\LLF_LifeTables.do"'

*******************************************************
* (4.0) Age at Marriage and Birth Timing 
* 		Figures A13a-b: Change in age of marriage
* 		Figures A14a-b: Change in age of first birth
* 		Figures A15a-d: Change in birth intervals
* 		Table A5: 		Change in age of marriage and first birth 
*******************************************************	
	do `"$d_do\LLF_Age_Marriage.do"'
	do `"$d_do\LLF_Age_FirstBirth.do"'
	do `"$d_do\LLF_BirthIntervals.do"'
	
*******************************************************
* (5.0) Sex Selective Fertility Strategies
* 		Figure 4: 		Sex selection strategy use 	
*		Figures A17a-f:	Sex selection - leave 2 out 	
* 		Table 1: 		Sex based discontinuation
* 		Table 2: 		Probability of male birth
* 		Table 3:		Male biased stopping rules 
* 		Table A9: 		Sex ratio at ages 0-5
*		Table A10: 		Breastfeeding back-of-the-envelope
* 		Table A11: 		Sex selection robustness 
*******************************************************
	
	do `"$d_do\LLF_SexSelection.do"'
	do `"$d_do\LLF_SexSelection_Leave2Out.do"'
	do `"$d_do\LLF_Breastfeeding.do"'
	
*******************************************************
* (6.0) Underreporting
* 		Figure A3: 	Underreporting 
* 		Figure A4: 	Underreporting
* 		Figure A5: 	Underreporting
* 		Figure A6: 	Underreporting	
*******************************************************
	do `"$d_do\LLF_Underreporting.do"'
	
		


	


	